//
//  HomeTableReusableView.m
//  PPDL
//
//  Created by 王洋 on 2019/7/23.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "HomeBannerCell.h"
#import "HomeMessageCell.h"
#import "HomeMainIconItemCell.h"
#import "HomeNormalImageCell.h"
#import "HomeDistanceCell.h"

#import "HomeTableReusableView.h"

@interface HomeTableReusableView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *bannerPicArray;
@property (nonatomic, strong) NSMutableArray *msgListArray;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) CGFloat mainIconItemHeight;

@end

@implementation HomeTableReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self layout];
    }
    return self;
}

- (void)layout {
    
    self.mainIconItemHeight = homeMainIconHeight;
    
    [self addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
}

- (void)fillHeaderWith:(HomeHeadModel *)model {

    //根据model拼装数据
    self.model = model;
    
    self.bannerPicArray = @[].mutableCopy;
    self.msgListArray = @[].mutableCopy;
    for (HomeHeadItemModel *bannerModel in self.model.bannerModel.itemArray) {
        [self.bannerPicArray addObject:bannerModel.itemImagePath];
    }
    
    for (HomeHeadItemModel *msgModel in self.model.notiMsgModel.itemArray) {
        [self.msgListArray addObject:msgModel.itemContent];
    }

    
    NSInteger lineCount = self.model.mainIconModel.itemArray.count/4 + (self.model.mainIconModel.itemArray.count%4>0?1:0);
    self.mainIconItemHeight = lineCount * homeMainIconHeight;
    
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        return PP_Home_Banner_Scale * TTScreenWidth ;
        
    } else if (indexPath.row == 1 ) {
        
        return self.mainIconItemHeight;

    } else if (indexPath.row == 3 ) {
        
        return homeNormalImageHeight;
        
    } else {

        return 10;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WEAKSELF

    if (indexPath.row == 0) {
                
        HomeBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeBannerCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        [cell configBannerCellWithImageArray:self.bannerPicArray];
        
        cell.CycleImgClickedBlock = ^(NSInteger index) {

            if (weakSelf.headerActionHandleBlock) {
                weakSelf.headerActionHandleBlock(self.model.bannerModel.itemArray[index].itemActionModel);
            }
        };

        return cell;

    } else if (indexPath.row == 1) {
        
        HomeMainIconItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeMainIconItemCellID" forIndexPath:indexPath];
        
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        [cell fillCellWithModel:self.model.mainIconModel];
        
        cell.MainIconClickedBlock = ^(NSInteger index) {
            
//            NSLog(@"%ld",(long)index);
            if (weakSelf.headerActionHandleBlock) {
                weakSelf.headerActionHandleBlock(self.model.mainIconModel.itemArray[index].itemActionModel);
            }
        };
        
        return cell;


    } else if (indexPath.row == 3 ) {
        
        HomeNormalImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeNormalImageCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;

        return cell;
    }
    
    else {
        
        HomeDistanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDistanceCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;

        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    HomeItemActionModel *model = [[HomeItemActionModel alloc] init];
    
    if (indexPath.row == 0) {  //banner
        
        NSLog(@"banner");
        return;

    } else if (indexPath.row == 1 ) {   //mainicon
        
        NSLog(@"mainIcon");
        return;

    } else if (indexPath.row == 3 ) {   //normal img
        
        NSLog(@"normal");

//        model.type = @"1";

    } else {
        
        NSLog(@"else");

    }
    
//    if (self.headerActionHandleBlock) {
//        self.headerActionHandleBlock(model.jumpModel);
//    }
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;

//        _tableView.estimatedSectionHeaderHeight = 0;
//        _tableView.estimatedSectionFooterHeight = 0;
//        _tableView.estimatedRowHeight = 0;
        
//        _tableView.tag = 1000;
        
        [_tableView registerNib:[UINib nibWithNibName:@"HomeMainIconCell" bundle:nil] forCellReuseIdentifier:@"HomeMainIconCellID"];
        
        
        [_tableView registerClass:[HomeMainIconItemCell class] forCellReuseIdentifier:@"HomeMainIconItemCellID"];

        [_tableView registerNib:[UINib nibWithNibName:@"HomeMessageCell" bundle:nil] forCellReuseIdentifier:@"HomeMessageCellID"];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HomeNormalImageCell" bundle:nil] forCellReuseIdentifier:@"HomeNormalImageCellID"];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HomeDistanceCell" bundle:nil] forCellReuseIdentifier:@"HomeDistanceCellID"];
        
        [_tableView registerClass:[HomeBannerCell class] forCellReuseIdentifier:@"HomeBannerCellID"];

    }
    
    return _tableView;
}
@end
